package com.lzjn.code.web.dao;

import com.lzjn.code.common.base.CommonDao;
import com.lzjn.code.common.model.PageModel;
import com.lzjn.code.common.util.PKUtil;
import com.lzjn.code.web.entity.DataSource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class DataSourceDao extends CommonDao<DataSource> {

	public PageModel getByPage(DataSource dataSource, PageModel page){
			
		StringBuilder sql = new StringBuilder();
		sql.append("select id,description,db_type,db_name,server,port,username,password ")
		   .append("from data_source where 1=1");
		
		List<Object> params = new ArrayList<>();
		
		if(StringUtils.isNotEmpty(dataSource.getDescription())) {
			
			sql.append(" and description like ?");
			params.add("%"+dataSource.getDescription()+"%");
		}
		if(StringUtils.isNotEmpty(dataSource.getDbName())) {
			
			sql.append(" and db_name like ?");
			params.add("%"+dataSource.getDbName()+"%");
		}
		
		page = queryByPage(sql.toString(), params, page);
		
		return page;
	}
	
	public List<DataSource> getList(){
		
		StringBuilder sql = new StringBuilder();
		sql.append("select id,description,db_type,db_name,server,port,username,password ")
		   .append("from data_source where 1=1");
		
		List<Object> params = new ArrayList<>();
		
		return query(sql.toString(), params);
	}
	
	public DataSource getById(String id){
		
		StringBuilder sql = new StringBuilder();
		sql.append("select id,description,db_type,db_name,server,port,username,password ")
		   .append("from data_source where id = ?");
		
		return queryById(sql.toString(), id);
	}
	
	public void add(DataSource dataSource) throws Exception {
		
		StringBuilder sql = new StringBuilder();
		sql.append("insert into data_source")
		   .append("(id,description,db_type,db_name,server,port,username,password) ")
		   .append("values(?,?,?,?,?,?,?,?)");
		
		List<Object> params = new ArrayList<>();
		params.add(PKUtil.getUUID());
		params.add(dataSource.getDescription());
		params.add(dataSource.getDbType());
		params.add(dataSource.getDbName());
		params.add(dataSource.getServer());
		params.add(dataSource.getPort());
		params.add(dataSource.getUsername());
		params.add(dataSource.getPassword());
		
		executeUpdate(sql.toString(), params);
	}
	
	public void update(DataSource dataSource) throws Exception {
		
		StringBuilder sql = new StringBuilder();
		sql.append("update data_source ")
		   .append("set description = ?,db_type = ?,db_name = ?,server = ?,port = ?,username = ?,password = ? ")
		   .append("where id = ?");
		
		List<Object> params = new ArrayList<>();
		params.add(dataSource.getDescription());
		params.add(dataSource.getDbType());
		params.add(dataSource.getDbName());
		params.add(dataSource.getServer());
		params.add(dataSource.getPort());
		params.add(dataSource.getUsername());
		params.add(dataSource.getPassword());
		params.add(dataSource.getId());
		
		executeUpdate(sql.toString(), params);
	}
	
	public void delete(String id) throws Exception {
		
		String sql = "delete from data_source where id = ?";
		
		List<Object> params = new ArrayList<>();
		params.add(id);
		
		executeUpdate(sql.toString(), params);
	}
	
	public void batchDelete(String ids) throws Exception {
		
		ids = ids.replace(",", "','");
		
		String sql = "delete from data_source where id in ('" + ids + "')";
		
		executeUpdate(sql, null);
	}
}
